package cn.eonshine.cuba.inf.modular.basic.service.impl;

import java.util.Date;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.eonshine.cuba.inf.common.http.InfStatus;
import cn.eonshine.cuba.inf.modular.basic.entity.Mapping;
import cn.eonshine.cuba.inf.modular.basic.entity.Oil;
import cn.eonshine.cuba.inf.modular.basic.mapper.OilMapper;
import cn.eonshine.cuba.inf.modular.basic.service.IOilService;
import cn.eonshine.cuba.inf.modular.common.service.AbstractCommonService;

/**
 * <p>
 * Oil（油品表） 服务实现类
 * </p>
 *
 * @author Alan
 * @since 2018-07-28
 */
@Service
public class OilServiceImpl extends AbstractCommonService<OilMapper, Oil> implements IOilService {

	/**   
	 * <p>Title: customerQueryWrapper</p>   
	 * <p>Description: </p>   
	 * @see cn.eonshine.cuba.inf.modular.common.service.AbstractCommonService#customerQueryWrapper()   
	 * @author: Alan
	 * @date:   2019-03-25 21:40:00
	 */
	@Override
	public QueryWrapper<Oil> customerQueryWrapper() {
		QueryWrapper<Oil> entity = new QueryWrapper<Oil>();
		entity.eq("lsms_update", InfStatus.LSMS_UPDATE_EXTRACT_NO);
		entity.orderByAsc("oil_code,create_date");
		return entity;
	}


	/**   
	 * <p>Title: transportDataHandle</p>   
	 * <p>Description: </p>   
	 * @param oil
	 * @param count
	 * @see cn.eonshine.cuba.inf.modular.common.service.AbstractCommonService#transportDataHandle(java.lang.Object, int)   
	 * @author: Alan
	 * @date:   2019-03-25 21:40:00
	 */
	@Override
	public int transportDataHandle(Oil oil, int count) throws Exception {
		String oilCode = oil.getOilCode();
		Date dataInputDateTime=oil.getDataInputDatetime();

		QueryWrapper<Oil> entity = new QueryWrapper<Oil>();
		entity.lambda().eq(Oil::getOilCode, oilCode);
		entity.lambda().eq(Oil::getDataInputDatetime, dataInputDateTime);
		boolean isSuccess = false;
		if(this.isUpdate==0){
			isSuccess = remove(entity);
		}else {
			isSuccess = update(oil,entity);
		}
		if (isSuccess) {
			count++;
		}
		return count;
	}


	/**   
	 * <p>Title: mappingHandle</p>   
	 * <p>Description: </p>   
	 * @param oil
	 * @throws Exception   
	 * @see cn.eonshine.cuba.inf.modular.common.service.AbstractCommonService#mappingHandle(java.lang.Object)   
	 * @author: Alan
	 * @date:   2019-03-25 21:40:00
	 */
	@Override
	public void mappingHandle(Oil oil) throws Exception {
		Mapping map = new Mapping();
		map.setLsmsId(oil.getId());
		map.setDmz1(oil.getOilCode());
		map.setName(oil.getOilName());
		map.setDataType(InfStatus.DMZ_MAPPING_OIL);
		
		QueryWrapper<Mapping> entitys = new QueryWrapper<Mapping>();
		entitys.eq("lsms_id", oil.getId());
		Mapping maps = mappingService.getOne(entitys);
		if (maps == null) {
			mappingService.save(map);
		}else {
			mappingService.update(map, entitys);
		}
	}


	/**   
	 * <p>Title: isUseCutomerSelectSQL</p>   
	 * @see cn.eonshine.cuba.inf.modular.common.service.AbstractCommonService#isUseCutomerSelectSQL()   
	 * @author: Alan
	 * @date:   2019-03-25 21:40:00
	 */
	@Override
	public boolean isUseCutomerSelectSQL() {
		return false;
	}

}
